<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
	/*
 *      Media.php
 *      
 *      Copyright 2011 godfather.typhoon.tycoon@banme <chungnguyenhuu911@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 *      
 *      
 */

	class  Video   extends  CI_Controller{
		private $pro_id;
		private $id;
		
		private $data = array(
				'dir' =>  'public/uploads/media/products/videos/',
				'dir_image' =>  'public/uploads/media/products/videos/images/',
				'total' => 0,
				'videos' => array(),
				'error' => ''
			);
		function  Video  (){
			
			}
		function __construct(){
			parent::__construct();
			$this->load->library('pagination');
			//$this->load->library('form_validation');
			$this->lang->load('messages','vietnamese'); 
			$this->lang->load('upload','vietnamese'); 
			/* ---------------------------------------------------
			 * Check admin login
			 *  
			*/ 
			if(!$this->session->userdata('go_admin')){
				redirect('admin/login', 'refesh');
				return;
			}
		}
		function index($start = 0){
			$this->pro_id = $this->uri->segment(3);
			$url = "";
			$url_image = ""; 
			/*
			* Vaidation data
			*/
			$this->form_validation-> set_rules('name', 'Name', 'required|xss_clean');
			$this->form_validation-> set_rules('name_en', 'Name En', 'required|xss_clean');
			
			$this->data['pro_id'] = $this->pro_id;
			
			$base_url = site_url('video/index/'.$this->pro_id);
			$per_page = $this->config->item('admin.product.video.page');
			$total_rows = $this->countVideo($this->pro_id);
			
			$config = $this->init_pagination($base_url, $per_page, $total_rows, 4);
			
			$this->data['videos'] = $this->findByProductID($this->pro_id, $config['per_page'], $this->uri->segment(4));
			$this->pagination->initialize($config);
			
			if ($this->form_validation->run() == FALSE){
				
				$this->data['main_content'] = 'manager/video';
				$this->load->view('templates/manager/template', $this->data);
			}else{
				
				$name = $this->input->post('name');
				$name_en = $this->input->post('name_en');
				
				$pro_id = $this->input->post('pro_id');
				/*
				*Set videos image path
				*/
				//get config file		
				$path = $this->config->item('product.video_image');
				$config = $this->config_upload_file($path, 'gif|jpg|png', 1024, 1024, 768, FALSE);
				
				/*
				*Upload image
				*/
				$this->load->library('upload', $config);
				$this->upload->initialize($config);
				if ( ! $this->upload->do_upload('userimage'))
				{
					$this->data['error_image'] = $this->upload->display_errors();
					
					$this->data['main_content'] = 'manager/video';
					$this->load->view('templates/manager/template', $this->data);
					return;
				}
				else
				{
					$data =  $this->upload->data('userimage');
					$url_image = $data['file_name'];					
				}
				
				/*
				*Set video path
				*/
				//get config file						
				$path2 = $this->config->item('product.video');
				$config1 = $this->config_upload_file($path2, 'flv|swf', 99920439, 1024, 768, FALSE);
				
				$this->load->library('upload', $config1);
				$this->upload->initialize($config1);
				
				if ( ! $this->upload->do_upload('userfile'))
				{
					//$this->data['error_video'] = "";
					$this->data['error_video'] = $this->upload->display_errors();
					$this->data['main_content'] = 'manager/video';
					$this->load->view('templates/manager/template', $this->data);
					return;
				}
				else
				{
					$data =  $this->upload->data('userfile');
					$url = $data['file_name'];
					
				}
				/*
				*Save data
				*/
				
				$this->insert($name, $name_en, $url, $url_image, $pro_id);
				redirect('video/index/'.$pro_id, 'refesh');
			}
		}
		
		function findByProductID($id = -1, $limit = NULL, $offset = NULL){
			$query = $this->db->get_where('video', array('product_id' => $id), $limit, $offset);
			return $query->result();

		}
		function countVideo($id = -1){
			$this->db->where('product_id', $id); 
			$this->db->from('video');
			return $this->db->count_all_results();
		}
		function edit(){
		$url = "";
		$url_image = "";
			$data = array();
			/*
			 * 
			 * redirect from list module
			 * Get ID
			 */ 
			 
			$id = $this->uri->segment(4);
			
			/*
			 * Check id
			 * if GET id param is null or empty
			 * Get  POST id param
			 */ 
			if($id == null || $id ==""){
				$id = $this->input->post('id');
			}
			$pro_id =  $this->uri->segment(3);
			if($pro_id == null || $pro_id ==""){
				$pro_id = $this->input->post('pro_id');
			}
			
			$this->form_validation-> set_rules('name', 'Name', 'required|xss_clean');
			$this->form_validation-> set_rules('name_en', 'Name En', 'required|xss_clean');

			$data['pro_id'] = $pro_id;
			$data['video'] = $this->findByID($id);
			if ($this->form_validation->run() == FALSE){
				$data['main_content'] = 'manager/editvideo';
				$this->load->view('templates/manager/template', $data);
			}else{
				$id = $this->input->post('id');
				$name = $this->input->post('name');
				$name_en = $this->input->post('name_en');
				$url_image = $data['video']['image'];
				$url = $data['video']['url'];
				$product_id = $pro_id;
				
				/*
				*Check image
				*/
				//if file not exist
				if($_FILES['userimage']["size"] <= 0 ){
					$url_image = $data['video']['image'];
				}else{
					$path = $this->config->item('product.video_image');
					$config = $this->config_upload_file($path, 'gif|jpg|png', 2048, 1024, 768, TRUE);
					
					$this->load->library('upload', $config);

					if ( ! $this->upload->do_upload('userimage'))
					{
						$data['error'] = $this->upload->display_errors();
						$url_image = $data['video']['image'];
						$data['main_content'] = 'manager/editvideo';
						$this->load->view('templates/manager/template', $data);
						return;
					}
					else
					{
						//delete old image
						$this->removieFile($this->config->item('product.video_image').$url_image);
						$data =  $this->upload->data('userimage');
						$url_image = $data['file_name'];
					}
				}
				/*
				 * Upload video
				 * //check has video
				 */ 
				if($_FILES['userfile']["size"] <= 0 ){
					$url = $data['video']['url'];
				}else{
					$path2 = $this->config->item('product.video');
					$config2 = $this->config_upload_file($path2, 'flv|swf', 99920439, 1024, 768, TRUE);
					$this->load->library('upload', $config2);

					if ( ! $this->upload->do_upload('userfile'))
					{
						$data['error'] = $this->upload->display_errors();
						$url = $data['video']['url'];
						$data['main_content'] = 'manager/editvideo';
						$this->load->view('templates/manager/template', $data);
						return;
					}
					else
					{
						//delete old image
						$this->removieFile($this->config->item('product.video').$url);
						$data =  $this->upload->data('userfile');
						$url = $data['file_name'];
						
					}
				}
				//update data
				$this->update($name, $name_en, $url, $product_id,$url_image, $id);
				redirect('video/index/'.$pro_id, 'refesh');
			}
		}
		function config_upload_file($path = "", $allowed_types = "", $max_size = -1, $max_width = -1, $max_height = -1, $override = FALSE){
			$config['upload_path'] = $path;
			$config['allowed_types'] = $allowed_types;
			$config['max_size']	= $max_size;
			$config['max_width']  = $max_width;
			$config['max_height']  = $max_height;
			$config['overwrite']  = $override;
			return $config;
		}
		function init_pagination($base_url = "", $per_page = 10, $total_rows = -1, $uri_segment){
			$config['base_url'] = $base_url;
			
			$config['per_page'] = $per_page;
			
			$config['total_rows'] = $total_rows;
			
			$config['uri_segment'] = $uri_segment;
			return $config;
		}
		function delete(){
			if($this->uri->segment(3) == "del" && $this->uri->segment(5) != "" && $this->uri->segment(4)){
			
				$pro_id =  $this->uri->segment(4);
				$id = $this->uri->segment(5);
				$video = $this->findByID($id);

				$this->remove($id, $video['url'], $video['image']);
				
				$config['base_url'] = site_url('video/index/'.$pro_id);
			
				$config['per_page'] = 2;
				
				$config['total_rows'] = $this->countVideo($pro_id);
				
				$config['uri_segment'] = '4';
			
				$this->data['videos'] = $this->findByProductID($pro_id, $config['per_page'], $id);
				$this->pagination->initialize($config);
				redirect('video/index/'.$pro_id, 'refesh');

			 }
		}
		function update($name = "", $name_en = "", $url = "", $product_id = -1,$image = "", $id = -1){
			$sql = "UPDATE `video` SET `name` = ?, `name_en` = ?, `url` = ?, `product_id` = ?, `image`= ?";
			$sql .= "WHERE `id` = ? ";
			$query = $this->db->query($sql, array($name, $name_en, $url, $product_id, $image, $id));
		}
		function insert($name = "", $name_en = "", $url = "", $image = "", $pro_id = -1){
			$sql = "INSERT INTO  `video` (`name`, `name_en`, `url`, `product_id`, `image`) VALUES (?, ?, ?, ?, ?)";
			$query = $this->db->query($sql, array($name, $name_en, $url, $pro_id, $image));
		}
		function remove($id, $url, $url_image){
			
			//remove videos image
			$this->removieFile($this->config->item('product.video_image'), $url_image);
			//remove videos
			$this->removieFile($this->config->item('product.video'), $url);
			//delete data
			
			$this->db->delete('video', array('id' => $id)); 
		}
		function removieFile($path_file, $name){
			$path = $path_file . $name;
			
			if(file_exists($path)){
				unlink($path);
			}
		}
		function findByID($id){
			$sql = "SELECT `id`,`name`, `name_en`, `url`, `product_id`, `image` FROM `video` WHERE `id` = ? limit 1";
			$query = $this->db->query($sql, array('id'=>$id)); 
			$data = array();
			if($query->num_rows() > 0){
				$data = $query->row_array();
			}
			$query->free_result();
			return $data;
		}
	}
?>
